import { Component, OnInit } from '@angular/core';
import { ConnectVO } from '../vo/ConnectVO';
import { UserVO } from '../vo/UserVO';
import { ApiUrl } from '../api/ApiUrl';
import { ApiService } from '../api/api.service';
import { Router } from '@angular/router';
import { AlertController, Item } from '@ionic/angular';
import { AnswerVO } from '../vo/Answervo';
import { ToastController } from '@ionic/angular';
@Component({
  selector: 'app-myconnectcompany',
  templateUrl: './myconnectcompany.page.html',
  styleUrls: ['./myconnectcompany.page.scss'],
})
export class MyconnectcompanyPage implements OnInit {

  constructor(public api: ApiService,  private router: Router,
    public alertController: AlertController, private toastController: ToastController) { }
  items: ConnectVO[] = [];
  userItem: UserVO;
  item: ConnectVO;
  ngOnInit() {
  this.userItem = ApiUrl.user;
  this.getItems();
  }
  getItems() {
    this.api.getItems<ConnectVO>(ApiUrl.getMyConnect, { 'iconsultant': this.userItem.iconsultantid }).subscribe(response => {
        if (response === null) {
          console.log('没有数据返回');
          return;
        }
        this.items = response;
        console.log(this.items);
      }, () => {

        console.log('出错了');
      });
  }
  goToDetails(i) {
    this.item = this.items[i];
    if (this.item.dconnecttime !== null) {
      this.router.navigate(['/connectdetail', {'iid': this.item.iid}]);
    } else {
      if ( this.item.iscompanyconnect === 2) {
        this.presentAlertConfirm1();
    }
    if ( this.item.isbankconnect === 2) {
      this.presentAlertConfirm(this.item.iid);
  }
    }
  }

  async presentAlertConfirm1() {
    const alert = await this.alertController.create({
      header: '信息与服务',
      message: '<strong>等待企业对接中...</strong>',
      buttons: [
        {
          text: '关闭',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah) => {
            console.log('Confirm Cancel: blah');
          }
        }
      ]
    });

    await alert.present();
  }
  async presentAlertConfirm(iid) {
    const alert = await this.alertController.create({
      header: '信息与服务',
      message: '<strong>是否联系该企业？</strong>',
      buttons: [
        {
          text: '关闭',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah) => {
            console.log('Confirm Cancel: blah');
          }
        }, {
          text: '发送',
          handler: () => {
             this.toConnect(iid);
          }
        }
      ]
    });

    await alert.present();
  }
  toConnect(iid) {
    this.api.getItem<AnswerVO>(ApiUrl.updateConnect, {
      'iid': iid, 'itype': '2'
    }).subscribe(response => {
      this.presentToast(response.msg);
    }, () => {
      console.log('出错了');
    });
  }

  async presentToast(msg) {
    const toast = await this.toastController.create({
      message: msg,
      duration: 2000
    });
    toast.present();
  }
}
